﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using ThuVien;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Printing;

namespace TranTekBE
{
    public partial class InGiaThuoc : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            this.Page.Form.DefaultButton = btnSubmit.UniqueID;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sql="null";
            try
            {
                string value_txtMaCode = txtMaCode.Text;
                string DivHtml;
                //
                // Split the string on line breaks.
                // ... The return value from Split is a string array.
                //
                string[] items_MaVach = Regex.Split(value_txtMaCode, "\r\n");
                // check price
                string check_gia = droplist_Gia.SelectedValue.ToString();
                
                // check mã POS trên textbox
                string[] items_POS = Regex.Split(value_txtMaCode, ";");
                string Ma_POS_textbox = items_POS[0];
                string type_MaCode = null;
                string MaCode = null;
                if (value_txtMaCode.Contains(";"))
                {
                    type_MaCode = "Sp.C001203";
                    MaCode = Ma_POS_textbox;
                }
                else
                {
                    type_MaCode = "Sp.C001205";
                    MaCode = items_MaVach[0];
                }
                //

                for (int i = 0; i < items_MaVach.Length; i++)
                {
                    sql = @"select top 10 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach'
                                 , Sp.C001215 as 'DVT' 
                                 , Gia.C000704 as 'Gia' from T0012 SP
                                Left Join T0007 Gia
                                on SP.C001200 = Gia.C000701
                                where " + type_MaCode + @" = '" + MaCode + @"' and Gia.C002000 = " + check_gia + @" order by Gia.C000704 asc";
                    //
                    DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);
                    
                    int count_rows = ds.Tables[0].Rows.Count;

                    if (ds != null && ds.Tables.Count > 0 && count_rows > 0)
                    {
                        string gia_SP = null;
                        
                        for (i = 1; i <= count_rows; i++) {
                            if (gia_SP == null)
                                gia_SP = ConvertMoneyVND(ds.Tables[0].Rows[i-1]["Gia"].ToString());
                            else
                            {
                                if(i%3==0)
                                    gia_SP = gia_SP + "</br>" + ConvertMoneyVND(ds.Tables[0].Rows[i-1]["Gia"].ToString());
                                else
                                    gia_SP = gia_SP + "/" + ConvertMoneyVND(ds.Tables[0].Rows[i - 1]["Gia"].ToString());

                            }
                        }
                        ltlDivDynamic1.Text = gia_SP.ToString();

                        //TextToImage();

                       // ConvertStringToImage();

                        txtMaCode.Text = null;
                        txtMaCode.Focus();

                    }

                 

                }//end for
            }// end try
            catch (Exception ex)
            {
                ltlError.Text = "<font color='red'>" + ex.Message + "</font>";
            }
        }
        public string ConvertMoneyVND( string input)
        {
            if (input != null)
            {
                return string.Format("{0:0,0}", Convert.ToDouble(input));
            }
            else
                return input;

        }

        

        private void TextToImage()
        {
            Color BackColor = Color.White;
            String FontName = "Times New Roman";
            int FontSize = 25;
            int Height = 50;
            int Width = 300;

            Bitmap bitmap = new Bitmap(Width, Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            Color color = Color.Gray; ;
            Font font = new Font(FontName, FontSize);

            SolidBrush BrushBackColor = new SolidBrush(BackColor);
            Pen BorderPen = new Pen(color);

            Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));

            graphics.FillRectangle(BrushBackColor, displayRectangle);
            graphics.DrawRectangle(BorderPen, displayRectangle);

            graphics.DrawString("1234", font, Brushes.Red, 0, 0);
            //imgTest.Image = bitmap;

           // System.Drawing.Image i = System.Drawing.Image.FromFile("E:\\print\\winterflowers2.bmp");
        Point p = new Point(100, 100);


        graphics.DrawImage(bitmap, p);

        }
    }
   
}